// pages/park-ticket/parkTicket.js
let request = require("../../utils/request.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    barHeight: wx.getMenuButtonBoundingClientRect().bottom,
    parkId: null,
    listLoading: false,
    btnLoading: false,
    currentPage: 1,
    pageSize: 10,
    pageInfo: { records: [], total: -1 },
    selectedParkTicketId: null,
    infoDialog: false,
    content: null,
    can : 1,
    shopCount:'',
    platformCount:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 
      parkId: options.parkId,
    })
    this.getParkTicket()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  
  /**
   * 获取商户停车券
   */
  getParkTicket() {
    request.http(
      '/platform/platform-ticket/page', 
      'GET', 
      { currentPage: 1, pageSize: 1000 },
      1,
      false
    ).then(res => {
      if(res.data.code == 200){
        res.data.data.records.push.apply(this.data.pageInfo.records, res.data.data.records);
        this.setData({ pageInfo: res.data.data, listLoading: false ,shopCount : res.data.data.records.length,can:1})
      }
    })
  },

  /**
   * 领取停车券
   */
  receiveParkTicket(e){
    let parkTicket = e.currentTarget.dataset.parkTicket
    this.setData({ btnLoading: true, selectedParkTicketId: parkTicket.id });
    request.http('/shop/shop-park-ticket/receive-platform-park-ticket', 'POST', 
    {shopId:wx.getStorageSync("shopId"),platformTicketId:parkTicket.id},1,
      false
    ).then(res => {
      if(res.data.code == 200){
        wx.showToast({ title: '领取成功' })
        this.getParkTicket()
        //  剩余数量减一
        // this.data.pageInfo.records.forEach((item, index)=> {
        //   if(item.id == parkTicket.id){
        //     let key = "pageInfo.records[" + index + "].remainingQuantity" 
        //     this.setData({
        //       [key]: parkTicket.remainingQuantity - 1
        //     })
        //   }
        // })
      }
      this.setData({ btnLoading: false })
    })
  },
  
  

 
  /**
   * 返回
   */
  back(){
    wx.navigateBack({ delta: 1 })
  }
})